/**
 * 
 */
package com.hanhai.zrb.api.biz.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hanhai.common.cache.Cache;
import com.hanhai.zrb.api.biz.service.BusinessCardService;
import com.hanhai.zrb.api.biz.service.CommonCounterService;
import com.hanhai.zrb.api.biz.service.FundService;
import com.hanhai.zrb.api.biz.service.IndustryService;
import com.hanhai.zrb.api.biz.service.IntentionService;
import com.hanhai.zrb.api.biz.service.ProjectService;
import com.hanhai.zrb.api.biz.service.UserDynamicService;
import com.hanhai.zrb.api.biz.service.UserInfoService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.api.client.facade.FundFacade;
import com.hanhai.zrb.model.choice.enums.ChoiceType;
import com.hanhai.zrb.model.choice.vo.ChoiceGeneralVo;
import com.hanhai.zrb.model.common.enums.StatusEnums;
import com.hanhai.zrb.model.fund.Fund;
import com.hanhai.zrb.model.fund.dto.FundDto;
import com.hanhai.zrb.model.fund.vo.AppFundVo;
import com.hanhai.zrb.model.project.Project;
import com.hanhai.zrb.model.user.User;
import com.hanhai.zrb.model.user.UserStatisticDetail;
import com.hanhai.zrb.model.user.enums.UserInfoName;
import com.hanhai.zrb.model.user.vo.BusinessCardVO;
import com.hanhai.zrb.model.user.vo.UserInfoVO;
import com.hanhai.zrb.model.userDynamic.UserDynamic;

/**
 * @author leiruiqi@hanhai.com
 *
 */

@Service("businessCardService")
public class BusinessCardServiceImpl implements BusinessCardService {

	@Resource
	private UserService userService;
	
	@Resource
	private UserInfoService userInfoService;
	
	@Resource
	private FundService fundService;
	
	@Resource
	private FundFacade fundFacade;
	
	@Resource
	private IndustryService industryService;
	
	@Resource
	private IntentionService intentionService;
	
	@Resource
	private ProjectService projectService;
	
	@Resource
	private Cache defaultCache;
	
	@Resource
	private CommonCounterService commonCounterService;
	
	@Resource
	private UserDynamicService userDynamicService;
	
	
	private String businessCardKey = "businessCardKey_";
	
	private ChoiceGeneralVoComparable choiceGeneralVoComparable = new ChoiceGeneralVoComparable();
	

	
	@Override
	public BusinessCardVO getUserBusinessCard(Long userId) {
		User user = userService.findById(userId);
		if (null != user) {
			BusinessCardVO busCardVo = new BusinessCardVO();

			busCardVo.setUid(user.getUid());
			busCardVo.setIndustryId(user.getIndustryId());
			busCardVo.setIndustryName(user.getIndustry());
			busCardVo.setRealName(user.getRealname());
			busCardVo.setUmobile(user.getUmobile());
			busCardVo.setOrg(user.getOrg());
			busCardVo.setAvatar(user.getAvatar());

			//获取用户职位
			List<UserInfoName> queryNameList = new ArrayList<UserInfoName>();
			queryNameList.add(UserInfoName.POSITION);
			UserInfoVO userInfoVo = userInfoService.findByNameEnums(
					Long.toString(user.getUid()), queryNameList);
			Map<String, String> infoMap = userInfoVo.getInfoMap();
			String position = infoMap.get("POSITION");
			busCardVo.setPosition(position);
			
			//获取用户的精选项目列表
			List<ChoiceGeneralVo> projectList = findUserChoiceProject(userId);
			//获取用户的精选资金列表
			List<ChoiceGeneralVo> fundList = findUserChoiceFund(userId);
			//合并list
			projectList.addAll(fundList);
			//排序
			this.sort(projectList);
			busCardVo.setChoiceGeneralList(projectList);
			busCardVo.setChoiceCount(projectList.size());
			
			//用户动态列表
			List<UserDynamic> userDynamicList = userDynamicService.queryAllByUserId(userId);
			removeExtra(userDynamicList);
			busCardVo.setDynamicList(userDynamicList);
			busCardVo.setDynamicCount(userDynamicList.size());
			
			return busCardVo;
		}
		return null;
	}
	private void removeExtra(List<UserDynamic> list){
		for(UserDynamic userDynamic:list){
			userDynamic.setExtra(null);
		}
	}
	public List<ChoiceGeneralVo> findUserChoiceProject(Long userId){
		List<ChoiceGeneralVo> choiceVoList = new ArrayList<ChoiceGeneralVo>();
		List<String> idList = new ArrayList<String>();
		List<Project> list = projectService.queryPageByOwnerId(String.valueOf(userId), null, 100,203);
		
		for(Project project:list){
			ChoiceGeneralVo vo = convertProjectToChoiceGeneralVo(project);
			choiceVoList.add(vo);
			idList.add(String.valueOf(vo.getBizId()));
		}
		

		Map<String,Long> countFundColMap = commonCounterService.listCountMapBySecondaryKeyList(UserStatisticDetail.Type.fund_col.getCountKey(), idList);
		
		Map<String,Long> countFundAdvisoryMap = commonCounterService.listCountMapBySecondaryKeyList(UserStatisticDetail.Type.fund_advisory.getCountKey(), idList);
		
		
		for(ChoiceGeneralVo vo:choiceVoList){
			String idStr = String.valueOf(vo.getBizId());
			Long countFundCol = countFundColMap.get(idStr);
			if(countFundCol == null){
				countFundCol = 0L;
			}
			vo.setCollectionCount(countFundCol);
			
			Long countFundAdvisory = countFundAdvisoryMap.get(idStr);
			if(countFundAdvisory == null){
				countFundAdvisory = 0L;
			}
			vo.setAdvisoryCount(countFundAdvisory);
		}
		return choiceVoList;
	}
	
	private ChoiceGeneralVo convertProjectToChoiceGeneralVo(Project project){
		ChoiceGeneralVo vo = new ChoiceGeneralVo();
		vo.setChoiceType(ChoiceType.project.getCode());
		vo.setBizId(project.getId());
		vo.setPic(project.getPic());
		vo.setPublishTime(project.getPushtime());
		vo.setAuth(project.getAuth());
		vo.setTitle(project.getProjectName());
		return vo;
	}
	
	
	public List<ChoiceGeneralVo> findUserChoiceFund(Long userId){
		List<ChoiceGeneralVo> choiceVoList = new ArrayList<ChoiceGeneralVo>();
		List<String> idList = new ArrayList<String>();

		FundDto dto = new FundDto();
		dto.setState(StatusEnums.RELEASE.getCode());
		
		dto.setUserId(userId);
		dto.setPageNum(1);
		dto.setPageSize(100);
		List<Fund> fundList = fundService.queryByCondition(dto);
		
		//List<AppFundVo> list = fundFacade.listAppFunds(userId, null, 100, false);
		for(Fund fund:fundList){
			ChoiceGeneralVo vo = convertFundToChoiceGeneralVo(fund);
			choiceVoList.add(vo);
			idList.add(String.valueOf(vo.getBizId()));
		}
		
		Map<String,Long> countFundColMap = commonCounterService.listCountMapBySecondaryKeyList(UserStatisticDetail.Type.pro_col.getCountKey(), idList);
		
		Map<String,Long> countFundAdvisoryMap = commonCounterService.listCountMapBySecondaryKeyList(UserStatisticDetail.Type.pro_advisory.getCountKey(), idList);
		
		
		for(ChoiceGeneralVo vo:choiceVoList){
			String idStr = String.valueOf(vo.getBizId());
			Long countFundCol = countFundColMap.get(idStr);
			if(countFundCol == null){
				countFundCol = 0L;
			}
			vo.setCollectionCount(countFundCol);
			
			Long countFundAdvisory = countFundAdvisoryMap.get(idStr);
			if(countFundAdvisory == null){
				countFundAdvisory = 0L;
			}
			vo.setAdvisoryCount(countFundAdvisory);
		}
		return choiceVoList;
	}
	
	/*private ChoiceGeneralVo convertFundToChoiceGeneralVo(AppFundVo fundVo){
		ChoiceGeneralVo vo = new ChoiceGeneralVo();
		vo.setChoiceType(ChoiceType.fund.getCode());
		vo.setBizId(fundVo.getId());
		vo.setPic(fundVo.getPic());
		vo.setPublishTime(fundVo.getPushTime());
		vo.setAuth(fundVo.getAuth());
		vo.setTitle(fundVo.getTitle());
		return vo;
	}*/
	
	private ChoiceGeneralVo convertFundToChoiceGeneralVo(Fund fund){
		ChoiceGeneralVo vo = new ChoiceGeneralVo();
		vo.setChoiceType(ChoiceType.fund.getCode());
		vo.setBizId(fund.getId());
		vo.setPic(fund.getPic());
		vo.setPublishTime(fund.getPushTime());
		vo.setAuth(fund.getAuth());
		vo.setTitle(fund.getTitlesVal());
		return vo;
	}
	
	public void sort(List<ChoiceGeneralVo> list){
		Collections.sort(list, choiceGeneralVoComparable);
	}
	
	@Override
	public BusinessCardVO getUserBusinessCardFromCache(Long userId){
		
		BusinessCardVO bvo = getCardFromCache(userId);
		if(bvo==null){
			bvo = this.getUserBusinessCard(userId);
			if(bvo == null){
				return null;
			}
			setCard2Cache(userId,bvo);
		}
		return bvo;
		
	}
	
	private BusinessCardVO getCardFromCache(Long userId){
		
		Object o = defaultCache.getValue(businessCardKey+userId);
		if(o!=null&&o instanceof BusinessCardVO){
			BusinessCardVO businessCardVo = (BusinessCardVO)o;
			return businessCardVo;
		}
		return null;
		
	}
	
	private void setCard2Cache(Long userId,BusinessCardVO bvo){
		defaultCache.add(businessCardKey+userId, bvo, 2);
	}
	
	class ChoiceGeneralVoComparable implements Comparator<ChoiceGeneralVo>{

		@Override
		public int compare(ChoiceGeneralVo o1, ChoiceGeneralVo o2) {
			Date d1 = o1.getPublishTime();
			Date d2 = o2.getPublishTime();
			return -d1.compareTo(d2);
		}
		
	}
}
